package com.lmy.androidcustomview.view

import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.graphics.withSave
import com.lmy.androidcustomview.R
import com.lmy.androidcustomview.dp

/**
 * @author lmy
 * @功能: 范围裁切 和几何动画
 * @Creat 2/9/21 3:06 PM
 * @Compony 465008238@qq.com
 */

class CameraView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val BITMAP_SIZE = 200.dp
    private val BITMAP_PADDING = 100.dp
    private val bitmap = getAvatar(BITMAP_SIZE.toInt())
    private val camera = Camera()
    var topFlip = 0f
        set(value) {
            field = value
            invalidate()
        }
    var bootomFlip = 0f
        set(value) {
            field = value
            invalidate()
        }
    var flipRotation = 0f
        set(value) {
            field = value
            invalidate()
        }

    init {
        camera.setLocation(0f, 0f, -6f * resources.displayMetrics.density)
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onDraw(canvas: Canvas) {
        //把保存然后恢复
        //上半部分
        canvas.withSave {
            //canvas的扩展函数 会自动在前面和后面添加save和restore方法
            translate(BITMAP_PADDING + BITMAP_SIZE / 2, BITMAP_PADDING + BITMAP_SIZE / 2)
            rotate(-flipRotation)
            camera.save()//避免重新绘制导致 bootomFlip值改变
            camera.rotateX(topFlip)
            camera.applyToCanvas(canvas)
            camera.restore()//恢复原先状态
            clipRect(-BITMAP_SIZE, -BITMAP_SIZE, BITMAP_SIZE, 0f)
            rotate(flipRotation)
            translate(
                -(BITMAP_PADDING + BITMAP_SIZE / 2),
                -(BITMAP_PADDING + BITMAP_SIZE / 2)
            )
            drawBitmap(bitmap, BITMAP_PADDING, BITMAP_PADDING, paint)
        }
        //下半部分
        canvas.withSave {
            translate(BITMAP_PADDING + BITMAP_SIZE / 2, BITMAP_PADDING + BITMAP_SIZE / 2)
            rotate(-flipRotation)
            camera.save()//避免重新绘制导致 bootomFlip值改变
            camera.rotateX(bootomFlip)
            camera.applyToCanvas(canvas)
            camera.restore()//恢复原先状态
            clipRect(-BITMAP_SIZE, 0f, BITMAP_SIZE, BITMAP_SIZE)
            rotate(flipRotation)
            translate(
                -(BITMAP_PADDING + BITMAP_SIZE / 2),
                -(BITMAP_PADDING + BITMAP_SIZE / 2)
            )
            drawBitmap(bitmap, BITMAP_PADDING, BITMAP_PADDING, paint)
        }

    }

    private fun getAvatar(width: Int): Bitmap {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeResource(resources, R.drawable.ic_head, options)
        options.inJustDecodeBounds = false
        options.inDensity = options.outWidth
        options.inTargetDensity = width
        return BitmapFactory.decodeResource(resources, R.drawable.ic_head, options)
    }
}